<template>
    <div >
        <div v-if="init" >
            <div v-watermark="waterMark"   :style="waterMarkStyle"></div>
            <component :is="component"></component>
        </div>
    </div>
</template>
<script>
    import {Base64} from 'js-base64'
    import {fullPath,projectView} from "@/libs/lazyLoading";
    import config from "@/config";
    import {getStore} from "@/libs/utils/store";
    import {validatenull} from "@/libs/utils/validate";
    import {getSysTime} from "_p/basic/api/system-date";
    import moment from 'moment'
    export default {
        name:'blank-main',
        data(){
            return{
                component:null,
                componentPath:'',
                isFull:false,
                params:null,
                init:false,
                refreshTime:-1,
                refreshLock:false,
                ssoRefreshTime:-1
            }
        },
        computed:{
            loader(){
                if(this.componentPath!=''){
                    if(this.isFull){
                        return fullPath(this.componentPath);
                    }else{
                        return projectView(this.componentPath);
                    }
                }else{
                    return null;
                }
            },
            waterMarkStyle(){
                return {
                    height: 'calc(100% - 10px)',
                    width: 'calc(100% - 10px)',
                    position: 'fixed',
                    top:'10px',
                    left:'10px',
                    zIndex:99999,
                    pointerEvents:'none'
                }
            },
            waterMark(){
                if(!this.init){
                    return {text:''}
                }
                return {
                    text:this.$store.state.user.userInfo.username+'-'+this.$store.state.user.userInfo.userRealName
                }
            }
        },
        methods:{
            refreshToken() {
                this.refreshTime = setInterval(() => {
                    const token = getStore({
                        name: 'access_token'
                    });
                    if (validatenull(token)) {
                        return;
                    }

                    if ((this.expires_in - Date.now()) / 1000 <= 30 && !this.refreshLock) {
                        this.refreshLock = true;
                        this.$store
                            .dispatch('RefreshToken')
                            .catch(() => {
                                clearInterval(this.refreshTime)
                            });
                        this.refreshLock = false
                    }
                }, 10000);
                if (this.$store.getters.sso_access_token&&this.$store.getters.sso_access_token!=='') {
                    this.ssoRefreshTime = setInterval(()=>{
                        this.refreshLock = true;
                        this.$store
                            .dispatch('RefreshOssToken')
                            .catch(() => {
                                clearInterval(this.ssoRefreshTime)
                            });
                        this.refreshLock = false
                    }, 1000*60*5)
                }
            },
        },
        created(){
            const jsonData = JSON.parse(Base64.decode(this.$route.query._link,true));
            this.componentPath = jsonData.pageData.component;
            this.isFull = jsonData.pageData.isFull;
            this.params = jsonData.pageData.params;
            window.document.title = config.getTitle() +'-'+ jsonData.pageData.pageTitle;
            if(this.$store.getters.access_token==''||this.$store.getters.refresh_token==''||!this.$store.getters.expires_in){
                this.$store.commit('SET_ACCESS_TOKEN',jsonData.token.accessToken)
                this.$store.commit('SET_REFRESH_TOKEN',jsonData.token.refreshToken)
                this.$store.commit('SET_EXPIRES_IN',jsonData.token.expiresIn)
                this.$store.dispatch('GetUserInfo').then(()=>{
                    this.init = true;
                    this.refreshToken();
                })
                getSysTime().then(res => {
                    if (res.code === "0000" && res.data) {
                        this.time = moment(res.data.bhDate).format('YYYY-MM-DD');
                        this.$store.commit('updateBatchDate',this.time);
                    }
                })
            }else{
                this.refreshToken();
                this.init = true;
            }
        },
        mounted(){
            this.component = this.loader;
        }
    }
</script>
